package com.example.test4

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.test4.Data.Datasource
import com.example.test4.model.Topic

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        enableEdgeToEdge()
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                Surface(
                    modifier = Modifier
                        .fillMaxSize()
                        .statusBarsPadding(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    TopicGrid(
                        modifier = Modifier.padding(8.dp) // 网格整体边距
                    )
                }
            }
        }
    }
}

@Composable
fun TopicGrid(modifier: Modifier = Modifier) {
    LazyVerticalGrid(
        columns = GridCells.Fixed(2),
        verticalArrangement = Arrangement.spacedBy(8.dp), // 卡片垂直间距
        horizontalArrangement = Arrangement.spacedBy(8.dp), // 卡片水平间距
        modifier = modifier
    ) {
        items(Datasource.DataSource.topics) { topic ->
            TopicCard(topic)
        }
    }
}

@Composable
fun TopicCard(topic: Topic, modifier: Modifier = Modifier) {
    // 1. 卡片添加圆角（12.dp 可调整，数值越大圆角越明显）
    Card(
        modifier = modifier.fillMaxWidth(),
        shape = RoundedCornerShape(12.dp) // 卡片圆角
    ) {
        Row(
            modifier = Modifier.padding(0.dp), // 卡片内部总边距
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 2. 图片添加圆角（8.dp，比卡片圆角小，更有层次感）
            Image(
                painter = painterResource(id = topic.imageRes),
                contentDescription = stringResource(id = topic.name),
                modifier = Modifier
                    .size(68.dp) // 设计图指定尺寸
                    .clip(RoundedCornerShape(8.dp)), // 图片圆角（核心）
                contentScale = ContentScale.Crop
            )

            // 右侧内容区域（垂直排列文字和数量）
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(start = 16.dp), // 图片与文字间距16dp
                verticalArrangement = Arrangement.Center
            ) {
                // 主题名称
                Text(
                    text = stringResource(id = topic.name),
                    fontSize = 16.sp,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.onSurface,
                    maxLines = 1
                )

                // 课程数量+图标
                Row(
                    modifier = Modifier.padding(top = 8.dp), // 名称与数量间距8dp
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_grain),
                        contentDescription = "课程数量",
                        modifier = Modifier.size(16.dp),
                        tint = MaterialTheme.colorScheme.onSurfaceVariant
                    )
                    Text(
                        text = topic.availableCourses.toString(),
                        fontSize = 14.sp,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        modifier = Modifier.padding(start = 8.dp) // 图标与数字间距8dp
                    )
                }
            }
        }
    }
}

@Preview(showBackground = true, showSystemUi = true)
@Composable
fun TopicPreview() {
    MaterialTheme {
        val topic = Topic(
            name = R.string.photography,
            availableCourses = 321,
            imageRes = R.drawable.photography
        )
        TopicCard(topic = topic)
    }
}

@Preview(showBackground = true, showSystemUi = true)
@Composable
fun GridPreview() {
    MaterialTheme {
        TopicGrid()
    }
}